{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Selecting Series from DataFrame " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Single Series" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# conventional way to import pandas\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# read a dataset of UFO reports into DataFrame \n", "ufo = pd.read_table('http://bit.ly/uforeports', sep=',')\n", "\n", "# read a csv is equivalent to read_table, except it assumes a comma separator \n", "ufo = pd.read_csv('http://bit.ly/uforeports')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CityColors ReportedShape ReportedStateTime
0IthacaNaNTRIANGLENY6/1/1930 22:00
1WillingboroNaNOTHERNJ6/30/1930 20:00
2HolyokeNaNOVALCO2/15/1931 14:00
3AbileneNaNDISKKS6/1/1931 13:00
4New York Worlds FairNaNLIGHTNY4/18/1933 19:00
\n", "
" ], "text/plain": [ " City Colors Reported Shape Reported State Time\n", "0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00\n", "1 Willingboro NaN OTHER NJ 6/30/1930 20:00\n", "2 Holyoke NaN OVAL CO 2/15/1931 14:00\n", "3 Abilene NaN DISK KS 6/1/1931 13:00\n", "4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# examine first 5 rows \n", "ufo.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 Ithaca\n", "1 Willingboro\n", "2 Holyoke\n", "3 Abilene\n", "4 New York Worlds Fair\n", " ... \n", "18236 Grant Park\n", "18237 Spirit Lake\n", "18238 Eagle River\n", "18239 Eagle River\n", "18240 Ybor\n", "Name: City, Length: 18241, dtype: object" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select 'City' Series using bracket notation\n", "ufo['City']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(ufo['City'])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 Ithaca\n", "1 Willingboro\n", "2 Holyoke\n", "3 Abilene\n", "4 New York Worlds Fair\n", " ... \n", "18236 Grant Park\n", "18237 Spirit Lake\n", "18238 Eagle River\n", "18239 Eagle River\n", "18240 Ybor\n", "Name: City, Length: 18241, dtype: object" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select 'City' Series using dot(.) notation\n", "ufo.City" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Note__\n", "- Bracket notation will always work, whereas dot notation has **limitations**\n", "- Dot notation doesn't work if there are **spaces** in the Series name\n", "- Dot notation doesn't work if the Series has the same name as a **DataFrame method or attribute** (like 'head' or 'shape')\n", "- Dot notation can't be used to define the name of a **new Series** (see below)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CityColors ReportedShape ReportedStateTimeLocation
0IthacaNaNTRIANGLENY6/1/1930 22:00Ithaca, NY
1WillingboroNaNOTHERNJ6/30/1930 20:00Willingboro, NJ
2HolyokeNaNOVALCO2/15/1931 14:00Holyoke, CO
3AbileneNaNDISKKS6/1/1931 13:00Abilene, KS
4New York Worlds FairNaNLIGHTNY4/18/1933 19:00New York Worlds Fair, NY
\n", "
" ], "text/plain": [ " City Colors Reported Shape Reported State Time \\\n", "0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00 \n", "1 Willingboro NaN OTHER NJ 6/30/1930 20:00 \n", "2 Holyoke NaN OVAL CO 2/15/1931 14:00 \n", "3 Abilene NaN DISK KS 6/1/1931 13:00 \n", "4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00 \n", "\n", " Location \n", "0 Ithaca, NY \n", "1 Willingboro, NJ \n", "2 Holyoke, CO \n", "3 Abilene, KS \n", "4 New York Worlds Fair, NY " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# create a new 'Location' Series (must use bracket notation to define the Series name)\n", "ufo['Location'] = ufo.City + ', ' + ufo.State\n", "ufo.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple Series" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CityStateTime
0IthacaNY6/1/1930 22:00
1WillingboroNJ6/30/1930 20:00
2HolyokeCO2/15/1931 14:00
3AbileneKS6/1/1931 13:00
4New York Worlds FairNY4/18/1933 19:00
............
18236Grant ParkIL12/31/2000 23:00
18237Spirit LakeIA12/31/2000 23:00
18238Eagle RiverWI12/31/2000 23:45
18239Eagle RiverWI12/31/2000 23:45
18240YborFL12/31/2000 23:59
\n", "

18241 rows × 3 columns

\n", "
" ], "text/plain": [ " City State Time\n", "0 Ithaca NY 6/1/1930 22:00\n", "1 Willingboro NJ 6/30/1930 20:00\n", "2 Holyoke CO 2/15/1931 14:00\n", "3 Abilene KS 6/1/1931 13:00\n", "4 New York Worlds Fair NY 4/18/1933 19:00\n", "... ... ... ...\n", "18236 Grant Park IL 12/31/2000 23:00\n", "18237 Spirit Lake IA 12/31/2000 23:00\n", "18238 Eagle River WI 12/31/2000 23:45\n", "18239 Eagle River WI 12/31/2000 23:45\n", "18240 Ybor FL 12/31/2000 23:59\n", "\n", "[18241 rows x 3 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select multiple series from dataframe \n", "ufo[['City', 'State', 'Time']]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }